package cn.kgc.mushroomwebui.controller;

import cn.kgc.mushroomcommon.api.ManagerRemoteService;
import cn.kgc.mushroomcommon.pojo.ResultEntity;
import cn.kgc.mushroomcommon.pojo.User;
import cn.kgc.mushroomcommon.util.CrowdConstant;
import cn.kgc.mushroomcommon.util.CrowdUtils;
import cn.kgc.mushroomcommon.vo.UserSuccessVO;
import cn.kgc.mushroomcommon.vo.UserVO;
import org.bouncycastle.asn1.crmf.ProofOfPossession;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@RestController
@RequestMapping("/user/manager")
public class UserController {
    @Resource
    ManagerRemoteService managerRemoteService;


    @RequestMapping("/do/login")
    public ResultEntity<UserSuccessVO> login(User user) {
        String loginacct = user.getAccount();
        String userpswd = user.getPassword();
        ResultEntity<UserSuccessVO> loginResultEntity = managerRemoteService.login(loginacct, userpswd);
        System.out.println(loginResultEntity);
        if (loginResultEntity.getResult().equals(ResultEntity.FAILED)) {
            loginResultEntity.setMessage("账号或者密码错误");
        }
        return loginResultEntity;
    }

    @RequestMapping("/logout")
    public ResultEntity<String> logout(@RequestParam("token")String token,HttpSession session) {
        ResultEntity<String> resultEntity = managerRemoteService.logout(token);
        if (ResultEntity.FAILED.equals(resultEntity.getResult())) {
            throw new RuntimeException(resultEntity.getMessage());
        }
        session.invalidate();
        return resultEntity;
    }

    @RequestMapping("/send/code")
    @ResponseBody
    public ResultEntity<String> sendCode(@RequestParam("phone") String phone) {
        return managerRemoteService.sendCode(phone);
    }

    @RequestMapping("/register")
    public ResultEntity<String> register(@RequestBody User user){
        ResultEntity<String> register = managerRemoteService.register(user);
        return register;
    }

    @RequestMapping("/checkCode")
    public ResultEntity<String> checkCode(@RequestParam("code") String code,@RequestParam("phone") String phone) {
        return managerRemoteService.checkCode(code,phone);
    }


    @RequestMapping("/do/loginByPhone")
    public ResultEntity<UserSuccessVO> loginByPhone(User user) {
        String phone = user.getPhone();
        ResultEntity<UserSuccessVO> userByPhone = managerRemoteService.getUserByPhone(phone);
        System.out.println(userByPhone);
        if (userByPhone.getResult().equals(ResultEntity.FAILED)) {
            userByPhone.setMessage("账号或者密码错误");
        }
        return userByPhone;
    }

    @RequestMapping("/checkCodeByPhone")
    public ResultEntity<String> checkCodeByPhone(@RequestParam("code") String code,@RequestParam("phone") String phone) {
        return managerRemoteService.checkCodeByPhone(code,phone);
    }

}
